/*
 * CS_DUNGEON_ENABLE_MAP_CHANGE_READY.h
 *
 *  Created on: 2013. 8. 27.
 *      Author: jjaehuny
 */

#include "matching.h"

#include "../../engine.h"

#include "../../User.h"

#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"

namespace ProjectZ { namespace NCommand { namespace NMatching {

/*
 * 던전 맵 완료 후 다른 맵으로 이동 시 가능 여부 확인 패킷
 * auth : jjaehuny@gamevil.com
 */

void CS_DUNGEON_ENABLE_MAP_CHANGE_READY::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/* message
	 * CS_DUNGEON_ENABLE_MAP_CHANGE_READY	0x080C		2	unsigned short	맵 TID
	 */

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_DUNGEON_ENABLE_MAP_CHANGE_READY NULL == pSpace");
		return;
	}

	if (false == pSpace->IsDungeon())
	{
		DEBUGLOG("CS_DUNGEON_ENABLE_MAP_CHANGE_READY space is not single or party dungeion");
		return;
	}

	PREPARE_PACKET;

	int map_tid = 0;
	pPacket->ParseMsg("%2d", &map_tid);

	DEBUGLOG("CS_DUNGEON_ENABLE_MAP_CHANGE_READY");

	//
	//NLogic::Space * pSpace = pUser->GetSpace();
	const NLogic::Space::USERTREE & clsUserTree = pSpace->GetUserTree();
	if (true == clsUserTree.empty())
	{
		DEBUGLOG("CS_DUNGEON_ENABLE_MAP_CHANGE_READY true == clsUserTree.empty()");
		return;
	}

	if (pSpace->GetData()._mapTid == map_tid || true != pUser->GetBattle()->GetData()._enable_map_clear)
	{
		DEBUGLOG("CS_DUNGEON_ENABLE_MAP_CHANGE_READY ALREADY CHANGE MAP");
		return;
	}

	std::for_each(clsUserTree.begin(), clsUserTree.end(), [&](const std::pair<const int, ProjectZ::User *> & pair){
		User * pTargetUser = pair.second;
		pTargetUser->GetBattle()->GetData()._enable_map_clear = false;
	} );

	pSpace->GetData()._mapTid = map_tid;

	DEBUGLOG("CS_DUNGEON_ENABLE_MAP_CHANGE_READY SEND MAP TID : %d", pSpace->GetData()._mapTid);

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
//	pNoti->MsgCat("%2d%2d%2d%2d", pSpace->GetData()._mapTid, pUser->player_info.x, pUser->player_info.y, pSpace->GetData()._degree);
	pNoti->MsgCat("%2d%2d%2d%2d", pSpace->GetData()._mapTid, pUser->GetBattle()->GetData().x, pUser->GetBattle()->GetData().y, pSpace->GetData()._degree);
	pNoti->MakePacket(CMD_SN_DUNGEON_MAP_CHANGE);
	pSpace->SendAll(pNoti);

	pSpace->GetData()._enableCount = 0;
	pSpace->ClearMonsterTree();
	pSpace->ClearDeadMonsterTree();
	pSpace->GetData()._bSendMapChange = true;
}

} /*NBattle*/ } /*NCommand*/ } /*ProjectZ*/
